<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dung
 * Date: 2/16/14
 * Time: 11:11 PM
 * To change this template use File | Settings | File Templates.
 */
?>
<div id="maincp">
    <h1 class="h1">Link Manager</h1>
    <ul class="list">
        <li><a href="index.php?p=addlink" title="Add a link">Add Link</a></li>
    </ul>
    <p>This table will show all link list in database.</p>
    <table class="datatable">
        <thead>
        <tr>
            <td>Title</td>
            <td>Link</td>
            <td></td>
        </tr>
        </thead>
        <?php
        if (isset($_GET["page"])) {
            $page  = $_GET["page"];
        }else{
            $page=1;
        }
        $start = ($page-1) * 10;
        $link = LinkList($start);
        if(mysql_num_rows($link)){
        ?>

         <?php
        while($row_link=mysql_fetch_array($link))
        {
            ob_start();
            ?>
            <tbody>
            <tr>
                <td>{Title}</td>
                <td>{Linked}</td>
                <td><a class="text-dung" href="index.php?p=editlink&link_id={id}">Edit</a>
                    -
                    <a class="text-dung" href="index.php?p=deletelink&link_id={id}">Delete</a>
                </td>
            </tr>
            </tbody>
            <?php
            $s= ob_get_clean();
            $s=str_replace("{id}",$row_link["linked_id"],$s);
            $s=str_replace("{Title}",$row_link["title"],$s);
            $s=str_replace("{Linked}",$row_link["linked"],$s);
            echo $s;
            ?>
        <?php
        }
        }else{ ?>
            <tbody>
            <tr>
                <td style="text-align: center" class="no-result" colspan="4">
                    <?php echo "No Result!"; ?>
                </td>
            </tr>
            </tbody>
        <?php
        }
        ?>
    </table>
    <table width="100%">
        <tr>
            <td>
                <?php
                $qr="SELECT COUNT(linked_id) from dt_link";
                $result=mysql_query($qr);
                $row_result=mysql_fetch_array($result);
                $records=$row_result[0];
                $total_pages=ceil($records/10);
                if($total_pages>2){
                    echo "Pages ";
                    for ($i=1; $i<=$total_pages; $i++) {
                        echo "<a href='index.php?p=linklist&page=".$i."'>".$i."</a> ";
                    }
                }?>
            </td>
        </tr>
    </table>
    <a href="index.php?p=cpanel" title="Back to Dashboard">&laquo; Back to Dashboard.</a>
</div>